package api

import (
	"mxshop/app/mxshop_api/api/config"
	controllerGoods "mxshop/app/mxshop_api/api/internal/controller/goods/v1"
	controllerSms "mxshop/app/mxshop_api/api/internal/controller/sms/v1"
	controllerUser "mxshop/app/mxshop_api/api/internal/controller/user/v1"
	"mxshop/app/mxshop_api/api/internal/service"
	auth2 "mxshop/app/mxshop_api/api/pkg/auth"
	"mxshop/app/mxshop_api/api/pkg/auth/BasicAuth"
	"mxshop/app/mxshop_api/api/pkg/auth/JWTAuth"
	"mxshop/gmicro/server/restserver"
	"mxshop/gmicro/server/restserver/middlewares/auth"
)

func initRouter(g *restserver.Server, cfg *config.Config) {
	v1 := g.Group("/v1")
	baseGroup := v1.Group("/base")
	userGroup := v1.Group("/user")
	goodsGroup := v1.Group("/goods")
	bannerGroup := v1.Group("/banners")
	BrandGroup := v1.Group("/brands")
	CategoryBrandGroup := v1.Group("/categorybrands")
	CategoryGroup := v1.Group("/categorys")

	data, err := GetDataFactoryOr(cfg.Registry)
	if err != nil {
		panic(err)
	}
	serviceFactory := service.NewService(data, cfg.Sms, cfg.Jwt)

	jwtAuth := JWTAuth.NewJWTAuth(cfg.Jwt) // 做 jwt 校验用
	//jwtStragy := jwtAuth.(auth.JWTStrategy)
	//jwtStragy.LoginHandler
	basicAuth := BasicAuth.NewBasicAuth(serviceFactory.User().User()) // 做 basic 校验用
	_ = auth.NewAutoStrategy(basicAuth, jwtAuth)                      // jwt 和 basic 自动适配

	userController := controllerUser.NewUserController(g.Translator(), serviceFactory)
	{
		userGroup.POST("pwd_login", userController.Login)
		userGroup.POST("register", userController.Register)
		userGroup.GET("detail", jwtAuth.AuthFunc(), userController.GetUserDetail)
		userGroup.PATCH("update", jwtAuth.AuthFunc(), userController.UpdateUser)
	}

	smsController := controllerSms.NewSmsController(g.Translator(), serviceFactory)
	{
		baseGroup.GET("captcha", controllerUser.GetCaptcha)
		baseGroup.POST("send_sms", smsController.SendSms)
	}

	goodsController := controllerGoods.NewGoodsController(g.Translator(), serviceFactory)
	{
		goodsGroup.GET("", goodsController.GoodsList)                                                        // 查看商品列表
		goodsGroup.POST("", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.GoodsNew)               // 创建商品
		goodsGroup.GET("/:id", goodsController.GoodsDetail)                                                  // 获取商品详情
		goodsGroup.DELETE("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.GoodsDelete)      // 删除商品
		goodsGroup.GET("/:id/stocks", goodsController.GoodsStocks)                                           // 获取商品的库存
		goodsGroup.PUT("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.GoodsUpdate)         // 更新商品
		goodsGroup.PATCH("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.GoodsUpdateStatus) // 更新商品状态
	}

	{
		bannerGroup.GET("", goodsController.BannerList)                                                   // 轮播图列表页
		bannerGroup.DELETE("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.BannerDelete) // 删除轮播图
		bannerGroup.POST("", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.BannerNew)          //新建轮播图
		bannerGroup.PUT("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.BannerUpdate)    //修改轮播图信息
	}

	{
		CategoryGroup.GET("", goodsController.CategoryList)                                                   // 商品类别列表页
		CategoryGroup.DELETE("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.CategoryDelete) // 删除分类
		CategoryGroup.GET("/:id", goodsController.CategoryDetail)                                             // 获取分类详情
		CategoryGroup.POST("", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.CategoryNew)          // 新建分类
		CategoryGroup.PUT("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.CategoryUpdate)    // 修改分类信息
	}

	{
		BrandGroup.GET("", goodsController.BrandList)                                                   // 品牌列表页
		BrandGroup.DELETE("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.BrandDelete) // 删除品牌
		BrandGroup.POST("", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.BrandNew)          //新建品牌
		BrandGroup.PUT("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.BrandUpdate)    //修改品牌信息
	}

	{
		CategoryBrandGroup.GET("", goodsController.CategoryBrandList)                                                   // 类别品牌列表页
		CategoryBrandGroup.GET("/:id", goodsController.CategoryBrandGetList)                                            // 获取分类的品牌
		CategoryBrandGroup.DELETE("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.CategoryBrandDelete) // 删除类别品牌
		CategoryBrandGroup.POST("", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.CategoryBrandNew)          // 新建类别品牌
		CategoryBrandGroup.PUT("/:id", jwtAuth.AuthFunc(), auth2.IsAdminAuth(), goodsController.CategoryBrandUpdate)    // 修改类别品牌
	}
}
